package com.study.talk.di

import android.content.Context
import com.study.talk.BuildConfig
import com.study.talk.data.local.AppDatabase
import com.study.talk.data.local.ChatCacheDao
import com.study.talk.data.local.GroupInfoDao
import com.study.talk.data.local.GroupMessageDao
import com.study.talk.data.local.MessageDao
import com.study.talk.data.local.UserInfoDao
import com.study.talk.data.remote.FileApi
import com.study.talk.data.remote.FriendApi
import com.study.talk.data.remote.GroupApi
import com.study.talk.data.remote.MomentApi
import com.study.talk.data.remote.MsgApi
import com.study.talk.data.remote.UserApi
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object AppModule {

    private const val IP = BuildConfig.SERVER_IP
    private const val NETTY_PORT = 10001
    private const val WEB_PORT = 10002

    private var token: String = ""

    private val okHttpClient = OkHttpClient.Builder().addInterceptor(tokenIntercept()).build()

    fun getIpAddress(): String {
        return IP
    }

    fun getNettyPort(): Int {
        return NETTY_PORT
    }

    private fun getWebPort(): Int {
        return WEB_PORT
    }

    fun getWebUrl(): String {
        return "http://${getIpAddress()}:${getWebPort()}"
    }

    fun getToken():String{
        return token
    }

    fun setToken(token:String){
        this.token = token
    }

    private fun tokenIntercept(): Interceptor {
        return Interceptor { chain ->
            chain.proceed(
                chain.request().newBuilder().addHeader("Authorization", getToken()).build()
            )
        }
    }

    @Provides
    @Singleton
    fun provideRetrofit(): Retrofit {
        return Retrofit.Builder()
            .baseUrl(getWebUrl())
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    @Provides
    @Singleton
    fun provideUserApi(retrofit: Retrofit): UserApi {
        return retrofit.create(UserApi::class.java)
    }

    @Provides
    @Singleton
    fun provideFileApi(retrofit: Retrofit): FileApi {
        return retrofit.create(FileApi::class.java)
    }

    @Provides
    @Singleton
    fun provideFriendApi(retrofit: Retrofit): FriendApi {
        return retrofit.create(FriendApi::class.java)
    }

    @Provides
    @Singleton
    fun provideGroupApi(retrofit: Retrofit): GroupApi {
        return retrofit.create(GroupApi::class.java)
    }

    @Provides
    @Singleton
    fun provideMomentApi(retrofit: Retrofit): MomentApi {
        return retrofit.create(MomentApi::class.java)
    }

    @Provides
    @Singleton
    fun provideMsgApi(retrofit: Retrofit): MsgApi {
        return retrofit.create(MsgApi::class.java)
    }

    @Provides
    @Singleton
    fun provideDataBase(@ApplicationContext context: Context): AppDatabase {
        return AppDatabase.getInstance(context)
    }

    @Provides
    @Singleton
    fun provideChatCacheDao(database: AppDatabase): ChatCacheDao {
        return database.chatCacheDao()
    }

    @Provides
    @Singleton
    fun provideGroupInfoDao(database: AppDatabase): GroupInfoDao {
        return database.groupInfoDao()
    }

    @Provides
    @Singleton
    fun provideGroupMessageDao(database: AppDatabase): GroupMessageDao {
        return database.groupMessageDao()
    }

    @Provides
    @Singleton
    fun provideMessageDao(database: AppDatabase): MessageDao {
        return database.messageDao()
    }

    @Provides
    @Singleton
    fun provideUserInfoDao(database: AppDatabase): UserInfoDao {
        return database.userInfoDao()
    }
}